package com.wordlearning.app.controller;

import com.wordlearning.app.entity.WordSet;
import com.wordlearning.app.entity.Word;
import com.wordlearning.app.service.WordSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/word-sets")
public class WordSetController {

    private final WordSetService wordSetService;

    @Autowired
    public WordSetController(WordSetService wordSetService) {
        this.wordSetService = wordSetService;
    }

    @PostMapping
    public ResponseEntity<WordSet> createWordSet(@RequestBody WordSet wordSet) {
        try {
            WordSet createdWordSet = wordSetService.createWordSet(wordSet);
            return new ResponseEntity<>(createdWordSet, HttpStatus.CREATED);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }
    }

    @GetMapping
    public ResponseEntity<List<WordSet>> getAllWordSets() {
        List<WordSet> wordSets = wordSetService.getAllWordSets();
        return new ResponseEntity<>(wordSets, HttpStatus.OK);
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<WordSet>> getWordSetsByUserId(@PathVariable("userId") Long userId) {
        List<WordSet> wordSets = wordSetService.getWordSetsByUserId(userId);
        return new ResponseEntity<>(wordSets, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<WordSet> getWordSetById(@PathVariable("id") Long id) {
        return wordSetService.getWordSetById(id)
                .map(wordSet -> new ResponseEntity<>(wordSet, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PutMapping("/{id}")
    public ResponseEntity<WordSet> updateWordSet(@PathVariable("id") Long id, @RequestBody WordSet wordSet) {
        try {
            WordSet updatedWordSet = wordSetService.updateWordSet(id, wordSet);
            return new ResponseEntity<>(updatedWordSet, HttpStatus.OK);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<HttpStatus> deleteWordSet(@PathVariable("id") Long id) {
        try {
            wordSetService.deleteWordSet(id);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @PostMapping("/{wordSetId}/words/{wordId}")
    public ResponseEntity<WordSet> addWordToWordSet(@PathVariable("wordSetId") Long wordSetId, @PathVariable("wordId") Long wordId) {
        try {
            WordSet updatedWordSet = wordSetService.addWordToWordSet(wordSetId, wordId);
            return new ResponseEntity<>(updatedWordSet, HttpStatus.OK);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
        }
    }

    @DeleteMapping("/{wordSetId}/words/{wordId}")
    public ResponseEntity<WordSet> removeWordFromWordSet(@PathVariable("wordSetId") Long wordSetId, @PathVariable("wordId") Long wordId) {
        try {
            WordSet updatedWordSet = wordSetService.removeWordFromWordSet(wordSetId, wordId);
            return new ResponseEntity<>(updatedWordSet, HttpStatus.OK);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
        }
    }

    @GetMapping("/{wordSetId}/words")
    public ResponseEntity<List<Word>> getWordsInWordSet(@PathVariable("wordSetId") Long wordSetId) {
        try {
            List<Word> words = wordSetService.getWordsInWordSet(wordSetId);
            return new ResponseEntity<>(words, HttpStatus.OK);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
        }
    }

}